<template>
  <view id="register" class="page">
    <view class="loading-wrap">
      <view class="loading-box">
        <view class="loader1">   </view>
            <image class="logo-img" src="../../image/icon_loading.png" />      
        <view class="loading-text">建业车生活</view>
      </view>
    </view>
  </view>
</template>
<script>
import Taro from "@tarojs/taro";
import "./register.less";
import { setGlobalData, getGlobalData } from "../../globalData.js";
import bulterApi from "@souche2014/bulter-api";
const api = bulterApi({ partner: "IOParking" });
const openIdMock = "oRPxj5bi2JB-id96cJ_s1wBWmjAo";
const mobileMock = "18039670989";
const API_CarCode = process.env.IOParking.API_CARCODE;
export default {
  name: "register",
  data() {
    return {
      apiBaseUrl: process.env.IOParking.API_HOST, //请求头部
      moveCarCodeNo: "",
    };
  },
  components: {},
  mounted() {},

  onLoad: function (options) {
    console.log("二维码参数:", JSON.stringify(options));
    if (options.code) {
      //体验开发版本扫码获得参数
      this.moveCarCodeNo = decodeURIComponent(options.code);
    } else if (options.q) {
      //正式版扫码获得参数
      let qrUrl = decodeURIComponent(options.q);
      this.moveCarCodeNo = this.getQueryCode(qrUrl);
    }
    if (!getGlobalData("openId")) {
      this.wx_login();
    } else {
      this.getRegisterInfoByCode();
    }
  },
  created() {},
  methods: {
    getQueryCode(url) {
      return url.substr(url.lastIndexOf("code=") + 5);
    },
    wx_login() {
      api
        .getUserInfo({
          scope: "all",
        })
        .then((resp) => {
          if (resp.success) {
            console.log("微信authCode is：", JSON.stringify(resp));
            if (resp.data == "mock") {
              setGlobalData("openId", openIdMock);
              setGlobalData("mobile", mobileMock);
              this.getRegisterInfoByCode();
              return;
            }
            this.getInfos(resp.data);
          } else {
            console.log("err is ", resp.message, "err code is ", resp.code);
          }
        });
    },
    getInfos(authCode) {
      Taro.request({
        url: this.apiBaseUrl + "/weixin/souchegetuser?authCode=" + authCode,
        data: {},
        header: {
          "content-type": "application/json", // 默认值
        },
        method: "GET",
        success: (res) => {
          console.log("获取用户信息：", JSON.stringify(res));
          if (res.data.isSuccess) {
            var userOpenId = res.data.data.openId;
            setGlobalData("openId", userOpenId);
            let mobileRes = res.data.data.phoneNum;
            if (mobileRes) {
              setGlobalData("mobile", mobileRes);
              this.mobile = mobileRes;
            } else {
              //授权获取手机号
              // this.hasPhone=false;
            }
            console.log("mobile=", this.mobile, mobileRes);
            this.getRegisterInfoByCode();
          }
        },
      });
    },
    //回到主页
    goHome() {
      api.openPage("home");
    },
    //解析链接方法
    getQueryString(url, name) {
      var reg = new RegExp("(^|&|/?)" + name + "=([^&|/?]*)(&|/?|$)", "i");
      var r = url.substr(1).match(reg);
      if (r != null) {
        return r[2];
      }
      return null;
    },
    //获取挪车码注册信息
    getRegisterInfoByCode() {
      console.log("挪车码参数", encodeURIComponent(this.moveCarCodeNo));
      let url =
        API_CarCode +
        "carmovingcode/getinfobycode?code=" +
        encodeURIComponent(this.moveCarCodeNo);
      Taro.request({
        url: url,
        data: {},
        header: {
          "content-type": "application/json", // 默认值
        },
        method: "GET",
        success: (res) => {
          console.log("挪车码注册信息：", JSON.stringify(res.data));
          if (res.data.isSuccess) {
            var userOpenId = res.data.data.openId;
            let carCodeNo = res.data.data.no;
            if (userOpenId) {
              console.log(
                "openId=---------",
                userOpenId,
                getGlobalData("openId")
              );
              if (userOpenId == getGlobalData("openId")) {
                //本人的
                Taro.redirectTo({ url: "../moveCarCode/myCode/myCode" });
              } else {
                //非本人的
                Taro.redirectTo({
                  url:
                    "../moveCarCode/virtualMobile/virtualMobile?no=" +
                    encodeURIComponent(carCodeNo),
                });
              }
            } else {
              //未注册的
              Taro.redirectTo({
                url: `../moveCarCode/registerCode/registerCode?no=${encodeURIComponent(
                  carCodeNo
                )}`,
              });
            }
          } else {
            Taro.showToast({
              title: res.data.message,
              icon: "error",
              duration: 1000,
              mask: true,
            });
          }
        },
      });
    },
    //end
  },
};
</script>

